package com.xd.Result;

import com.xd.erroCode.Errcode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
//该类的作用，标记该类的作用是实现序列化
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> implements Serializable {
    /**
     * 错误码
     *
     * @see
     */
    Integer code;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 错误提示，用户可阅读
     */
    private String msg;

    public static <T> CommonResult<T> Success(T data){
     CommonResult<T>  result  =new CommonResult<>();
     result.code=200;
     result.data=data;
     result.msg="请求成功";
     return result;
    }
    public static <T> CommonResult<T> error(Errcode errcode, T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = Integer.parseInt(errcode.getCode());
        result.msg = errcode.getMessage();
        result.data = data;
        return result;
    }
    public static <T> CommonResult<T> Success(){
        CommonResult<T> result = new CommonResult<>();
        result.code=200;
        result.msg="操作成功";
        return  result;
    }

    public static <T> CommonResult<T> error(Errcode errcode) {
        CommonResult<T> result = new CommonResult<>();
        result.code = Integer.parseInt(errcode.getCode());
        result.msg = errcode.getMessage();
        return result;
    }
    // 添加 isSuccess 方法
    public boolean isSuccess() {
        return code == 200;
    }

    public static <T> CommonResult<T> error(String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.msg = msg;
        return result;
    }






}
